package com.example.a517.a19tobar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity1 extends AppCompatActivity implements AdapterView.OnItemClickListener {
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        listView = findViewById(R.id.friend);
        /*好友界面*/
        ArrayList list = new ArrayList();
        Item item = new Item();
        item.setContent("我去充电了");
        item.setItem("一号");
        item.setImage(R.drawable.fish);
        list.add(item);

        item = new Item();
        item.setContent("我去睡觉了");
        item.setItem("二号");
        item.setImage(R.drawable.pkqone);
        list.add(item);

        item = new Item();
        item.setContent("我去洗澡了");
        item.setItem("三号");
        item.setImage(R.drawable.pkqtwo);
        list.add(item);

        item = new Item();
        item.setContent("我去吃饭了");
        item.setItem("四号");
        item.setImage(R.drawable.txal);
        list.add(item);

        item = new Item();
        item.setContent("我去上课了");
        item.setItem("五号");
        item.setImage(R.drawable.txx);
        list.add(item);

        item = new Item();
        item.setContent("求求你别烦我了");
        item.setItem("六号");
        item.setImage(R.drawable.sb);
        list.add(item);

        CharAdapter charAdapter = new CharAdapter(MainActivity1.this,list);
        listView.setAdapter(charAdapter);
        /*聊天界面*/
        listView.setOnItemClickListener(this);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(MainActivity1.this,MainActivity.class);

        startActivity(intent);
    }


    class CharAdapter extends BaseAdapter {

        /* 上下文对象，某个Activity*/
        private Context context;
        /* 要展示的数据*/
        private ArrayList<Item> list;

        public CharAdapter(Context context, ArrayList<Item> list) {
            this.context = context;
            this.list = list;
        }

        /**
         * 获取列表项的长度
         *
         * @return
         */
        @Override
        public int getCount() {
            /* 三元表达式：表达式1？表达式2：表达式3
             *  先判断表达式1，若结果为true,最终结果就是表达式2
             *                 若结果为false，最终结果就是表达式3*/
//        return list==null?0:list.size();
            if (list == null) {
                return 0;
            } else {
                return list.size();
            }
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /* 1、关联item布局文件
             *  2、关联控件
             *  3、设置控件的内容*/

        /* 数据量过大时，为了优化ListView减少耗时操作，利用ViewHolder类
            * 将要加载的子View放在ViewHolder类中，
              当第一次创建convertView时将这些控件找出，
              在第二次重用convertView时就可直接通过convertView中的getTag()方法获得这些控件*/

            ViewHolder viewHolder;
            if (convertView == null) {

                viewHolder = new ViewHolder();
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                convertView=layoutInflater.inflate(R.layout.item,parent,false);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.content = convertView.findViewById(R.id.content);
            viewHolder.item = convertView.findViewById(R.id.item);
            viewHolder.img = convertView.findViewById(R.id.img);

            viewHolder.content.setText(list.get(position).getContent());
            viewHolder.item.setText(list.get(position).getItem());
            viewHolder.img.setImageResource(list.get(position).getImage());



            return convertView;
        }

        class ViewHolder {
            TextView content;
            TextView item;
            ImageView img;
        }
    }
}
